const path = require('path')
const webpack = require('webpack')
const { VueLoaderPlugin } = require('vue-loader')
// 自动生成html插件
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
  entry: './src/main.js', // 入口文件
  target: 'web',
  output: { // 输出文件
    filename: '[name]-[contenthash:8]bundle.js',
    path: path.join(__dirname, 'dist')
  },
  mode: 'none',
  devServer: {
    hot: true, // 配置热更新
    hotOnly: true, // 启用热模块替换
    contentBase: path.join(__dirname, 'public')
  },
  module: {
    // loader配置
    rules: [
      {
        test: /.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: [
              ['@babel/preset-env']
            ]
          }
        }
      },
      {
        test: /(.js|.vue)$/,
        exclude: /node_modules/,
        use: 'eslint-loader',
        enforce: 'pre'
      },
      {
        test: /.less$/,
        use: [
          'vue-style-loader',
          'css-loader',
          'less-loader'
        ]
      },
      {
        test: /\.css$/,
        use: [
          'vue-style-loader',
          'css-loader'
        ]
      },
      {
        test: /.vue$/,
        use: {
          loader: 'vue-loader'
        }
      },
      {
        test: /\.(png|jpg|gif)$/,
        use: {
          loader: "url-loader",
          options: {
            limit: 10 * 1024,
            esModule: false
          }
        }
      }
    ]
  },
  plugins: [
    new VueLoaderPlugin(),
    new webpack.HotModuleReplacementPlugin(), // 热更新需要的插件
    new webpack.DefinePlugin({
      BASE_URL: JSON.stringify('./')
    }),
    new HtmlWebpackPlugin({
      title: 'webpack plugin test',
      template: './public/index.html'
    })
  ]
}